package com.example.chenhaosblog.controller;

import com.example.chenhaosblog.base.BaseController;
import com.example.chenhaosblog.service.DemoService;
import com.example.chenhaosblog.utils.DataUtil;
import com.example.chenhaosblog.utils.DateUtil;
import com.example.chenhaosblog.utils.PageData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * demo 模块
 */
@Controller
@RequestMapping("/demoController")

public class DemoController extends BaseController {

	private static Logger logger = LoggerFactory.getLogger(DemoController.class);

	@Autowired
	private DemoService demoService;

      public String test(){
		  PageData pageData = getPageData();
		  try {
			  List<PageData> all= demoService.findAll(pageData);

		  } catch (Exception e) {
			  e.printStackTrace();
		  }

		  return "成功";
	  }





	/**
	 *   查询数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/view")
	@ResponseBody
	public PageData findView() throws Exception{
		//返回结果
		PageData result = new PageData();
		//参数
		PageData pageData = getPageData();
		try {

			//PageData data=	demoService.findView(pageData);

            ArrayList<PageData>  users  =  queryUsers();
            for  (PageData  user:  users) {

                System.out.println("user:"  +  user.toString());
            }


			result.put("code", "00");
			//result.put("data",data);
			result.put("msg", "查询成功");
		} catch (Exception e) {
			logger.error(e.toString());
			throw new Exception(e.toString());
		}
		return result;

	}

    /**
     *模拟批量查询用户场景
     *  @return
     */
    private  ArrayList<PageData>  queryUsers() {
        ArrayList<PageData> users = new ArrayList<>();
        Boolean flag = true;
        while (flag) {
            for (int i = 0; i <1000000 ; i++) {
                char[] chars4 = new char[100000 * 5048];
                char[] chars = new char[100000 * 2048];
                PageData pageData = new PageData();
                pageData.put("1", "232");
                pageData.put("3", chars);
                pageData.put("33", chars4);
                users.add(pageData);
            }


        }
        return    users;
    }




	/**
	 *   修改数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/update")
	@ResponseBody
	public PageData update() throws Exception{
		//返回结果
		PageData result = new PageData();
		//参数
		PageData pageData = getPageData();
		try {
			pageData.put("updateTime", DateUtil.getTime());
			demoService.update(pageData);
			result.put("code", "00");
			result.put("data", "");
			result.put("msg", "保存成功");
		} catch (Exception e) {
			logger.error(e.toString());
			throw new Exception(e.toString());
		}
		return result;

	}

	/**
	 *   新增数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/add")
	@ResponseBody
	public PageData add() throws Exception{

		//返回结果
		PageData result = new PageData();
		//参数
		PageData pageData = getPageData();
		try {
			pageData.put("id",get32UUID());
			pageData.put("createTime", DateUtil.getTime());
			pageData.put("updateTime", DateUtil.getTime());
			demoService.add(pageData);

			result.put("code", "00");
			result.put("data", "");
			result.put("msg", "保存成功");
		} catch (Exception e) {
			logger.error(e.toString());
         //SSSSthrow new RuntimeException(e.getMessage());
        }
		return result;

	}

	/**
	 *   单个删除数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/del")
	@ResponseBody
	public PageData del() throws Exception{
		//返回结果
		PageData result = new PageData();
		//参数
		PageData pageData = getPageData();
		try {
			pageData.put("isDelete",1);
			pageData.put("updateTime", DateUtil.getTime());
			demoService.del(pageData);
			result.put("code", "00");
			result.put("data", "");
			result.put("msg", "删除成功");
		} catch (Exception e) {
			logger.error(e.toString());
			throw new Exception(e.toString());
		}
		return result;

	}


	/**
	 *   批量删除
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/dels")
	@ResponseBody
	public PageData dels() throws Exception{
		//返回结果
		PageData result = new PageData();
		//参数
		PageData pageData = getPageData();
		String[] ids = pageData.getString("ids").split(",");

		try {
			List<String> idList= new ArrayList<>(Arrays.asList(ids));

			pageData.put("updateTime", DateUtil.getTime());
			demoService.dels(idList);
			result.put("code", "00");
			result.put("data", "");
			result.put("msg", "删除成功");
		} catch (Exception e) {
			logger.error(e.toString());
			throw new Exception(e.toString());
		}
		return result;

	}

	
	/**
	 * 分页功能
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getPageDemoList")
	@ResponseBody
	public String getPageDemoList()
			throws Exception {
		PageData pageData =getPageData();
		PageHelper.startPage(Integer.parseInt(pageData.getString("page"))  , Integer.parseInt(pageData.getString("limit")));//页面的位置和展示的数据条目数
		String  data="";
		try {

			List<PageData> all = demoService.findAll(pageData);
			PageInfo<PageData> pageInfo = new PageInfo<PageData>(all);
			data= DataUtil.getJsonData(pageInfo);

		} catch (Exception e) {
			logger.error(e.toString());
			throw new Exception(e.toString());
		}


		return data;
	}

	
}
